<?php
require (Yii::app ()->basePath . '/lib/rolling-curl/RollingCurl.php');
/**
 *
 * @author firm
 * @see 企业信息管理
 */
class EnterpriseController extends CController {
	/**
	 *
	 * @see 企业信息管理-企业信息列表
	 */
	function actionList() {
		$this->render ( 'list' );
	}
	/**
	 *
	 * @see ajax 列表
	 */
	function actionAjax_enterprise_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		$service = new EnterpriseEntity ();
		$pg = $_GET ['page'];
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$where = array (
				'deleted' => 0 
		);
		$firm_name = $_GET ['fname'];
		sys_debug_log ( $firm_name );
		if ($firm_name != "") {
			$where ['firm_name'] = "[code] like '%$firm_name%' [code]";
		}
		fb ( $where );
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		$ret->tct = $totalCount;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		$ret->pi = $pg;
		if ($list === false) {
			$ret->status = 500;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 200;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 企业信息管理-企业信息列表-增加
	 */
	function actionAdd() {
		$this->render ( 'add' );
	}
	/**
	 *
	 * @see ajax提交
	 */
	public function actionAjax_enterprise_add() {
		$ret = new stdClass ();
		$ret->status = 401;
		$firm_name = $_POST ['firm_name'];
		$legal_representative = $_POST ['legal_representative'];
		$address = $_POST ['address'];
		$capital = $_POST ['capital'];
		$phone = $_POST ['phone'];
		$level = $_POST ['level'];
		$register_time = $_POST ['register_time'];
		$fld2 = $_POST ['fld2'];
		$name2 = $_POST ['name2'];
		$name3 = $_POST ['name3'];
		$name4 = $_POST ['name4'];
		$remark = $_POST ['remark'];
		$level = $_POST ['level'];
		$fldvalue = array (
				'firm_name' => $firm_name,
				'legal_representative' => $legal_representative,
				'capital' => $capital,
				'address' => $address,
				'phone' => $phone,
				'level' => $level,
				'register_time' => $register_time,
				'fld2' => $fld2,
				'name2' => $name2,
				'name3' => $name3,
				'name4' => $name4,
				'remark' => $remark,
				'add_time' => date ( 'Y-m-d H:i:s', time () ),
				'deleted' => 0 
		);
		$service = new EnterpriseEntity (); // 定义用户业务数据库操作对象
		$result = $service->insert ( $fldvalue );
		if (is_numeric ( $result ) && $result > 0) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see ajax 删除企业信息
	 */
	function actionAjax_enterprise_delete() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new EnterpriseEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see ajax更新
	 */
	public function actionAjax_enterprise_editor() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		
		$id = $_POST ['id'];
		$firm_name = $_POST ['firm_name'];
		$legal_representative = $_POST ['legal_representative'];
		$address = $_POST ['address'];
		$capital = $_POST ['capital'];
		$phone = $_POST ['phone'];
		$level = $_POST ['level'];
		$fld2 = $_POST ['fld2'];
		$name2 = $_POST ['name2'];
		$name3 = $_POST ['name3'];
		$name4 = $_POST ['name4'];
		$remark = $_POST ['remark'];
		$register_time = $_POST ['register_time'];
		$fldvalue = array (
				'firm_name' => $firm_name, // 企业名称
				'address' => $address, // 地址
				'phone' => $phone, // 电话
				'capital' => $capital, // 注册资金
				'level' => $level, // 资质等级
				'fld2' => $fld2, // 资质授予时间
				'legal_representative' => $legal_representative, // 法人代表人
				'name2' => $name2, // 技术负责人
				'name3' => $name3, // 法人经营负责人
				'name4' => $name4, // 财务负责人,
				'register_time' => date ( 'Y-m-d H:i:s', time () ),
				'remark' => $remark 
		);
		
		$service = new EnterpriseEntity ();
		$result = $service->update ( $fldvalue, array (
				'id' => $id 
		) );
		if ($result === true) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see 企业信息管理-企业信息列表-编辑
	 */
	function actionEdit() {
		$id = $_GET ['id'];
		$service = new EnterpriseEntity ();
		$model = $service->get ( $id );
		if (false === $model || is_array ( $model ) === false || count ( $model ) === 0) {
			exit ( "参数错误" );
		}
		$this->render ( 'edit', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 企业信息管理-企业信息列表-详情
	 */
	public function actionDetail() {
		if (isset ( $_GET ['id'] ) && is_numeric ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$service = new EnterpriseEntity ();
			$model = $service->get ( $id );
			if (false === $model || is_array ( $model ) === false || count ( $model ) == 0) {
				// 参数错误
				exit ( "参数错误" );
			} else {
				$this->render ( 'detail', array (
						'model' => $model 
				) );
			}
		} else {
			exit ( "参数错误" );
		}
	}
	/**
	 *
	 * @see 企业信息管理-人员信息管理列表
	 */
	function actionPersonnel() {
		$id = $_GET ['id'];
		$this->render ( 'Personnel', array (
				'id' => $id 
		) );
	}
	
	/**
	 *
	 * @see ajax 列表
	 */
	function actionAjax_personnel_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		$service = new PersonnelEntity ();
		
		$pg = $_GET ['page'];
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$name = $_GET ['name'] or "";
		$sex = $_GET ['sex'] or "";
		$company =$_GET ['company'] or "";
		$card = $_GET ['card'] or "";
		$id = $_GET ['id'];
		if ($id > 0) {
			$where = array (
					'deleted' => 0,
					'enterprise_id' => $id 
			);
		} else {
			$where = array (
					'deleted' => 0 
			);
			if ($name != "") {
				$where ['name'] = "[code] like '%$name%' [code]";
			}
			if ($company_name != "") {
				$where ['company_name'] = "[code] like '%$company%' [code]";
			}
			if ($card_number != "") {
				$where ['card_number'] = "[code] like '%$card%' [code]";
			}
			if ($sex != "") {
				$where ['sex'] = $sex;
			}
		}
		$fld = array (
				'id',
				'enterprise_id', // 所在公司ID
				'name', // 人员姓名
				'birthday', // 出生日期
				'card_number', // 身份证号
				'level', // 职称等级
				'profession', // 专业
				'educational_background', // 学历
				'sex', // 性别
				'job_title', // 职称登记
				'job_title_number', // 职称批准文号
				'remark',
				'(select firm_name from company where id = enterprise_id limit 1) as company_name' 
		);
		
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		), $fld );
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		$ret->tct = $totalCount;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		$ret->pi = $pg;
		if ($list === false) {
			$ret->status = 500;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 200;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 企业信息管理-人员信息管理列表-增加
	 */
	function actionPersonnelAdd() {
		$id = $_GET ['id'];
		$service = new EnterpriseEntity ();
		$model = $service->get ( $id );
		if (false === $model || is_array ( $model ) === false || count ( $model ) === 0) {
			exit ( "参数错误" );
		}
		$this->render ( 'personnelAdd', array (
				'model' => $model 
		) );
	}
	/**
	 *
	 * @see ajax提交
	 */
	public function actionAjax_personnel_add() {
		$ret = new stdClass ();
		$ret->status = 401;
		$id = $_POST ['enterprise_id'];
		$name = $_POST ['name'];
		$birthday = $_POST ['birthday'];
		$card_number = $_POST ['card_number'];
		$educational_background = $_POST ['educational_background'];
		$profession = $_POST ['profession'];
		$job_title = $_POST ['job_title'];
		$level = $_POST ['level'];
		$job_title_number = $_POST ['job_title_number'];
		$sex = $_POST ['sex'];
		$remark = $_POST ['remark'];
		$fldvalue = array (
				'name' => $name,
				'sex' => $sex,
				'birthday' => $birthday,
				'card_number' => $card_number,
				'educational_background' => $educational_background,
				'profession' => $profession,
				'job_title' => $job_title,
				'level' => $level,
				'job_title_number' => $job_title_number,
				'remark' => $remark,
				'deleted' => 0,
				'enterprise_id' => $id 
		);
		$service = new PersonnelEntity (); // 定义用户业务数据库操作对象
		$result = $service->insert ( $fldvalue );
		if (is_numeric ( $result ) && $result > 0) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see ajax更新
	 */
	public function actionAjax_personnel_update() {
		$ret = new stdClass ();
		$ret->status = 401;
		$id = $_POST ['id'];
		$firm_name = $_POST ['firm_name'];
		$name = $_POST ['name'];
		$sex = $_POST ['sex'];
		fb ( $sex );
		$card_number=$_POST['card_number'];
		$birthday = $_POST ['birthday'];
		$level = $_POST ['level'];
		$profession = $_POST ['profession'];
		$educational_background = $_POST ['educational_background'];
		$job_title = $_POST ['job_title'];
		$job_title_number = $_POST ['job_title_number'];
		$remark = $_POST ['remark'];
		$fldvalue = array (
				'name' => $name,
				'birthday' => $birthday,
				'level' => $level,
				'card_number' =>$card_number,
				'profession' => $profession,
				'sex' => $sex,
				'educational_background' => $educational_background,
				'job_title' => $job_title,
				'job_title_number' => $job_title_number,
				'remark' => $remark 
		);
		$service = new PersonnelEntity ();
		$result = $service->update ( $fldvalue, array (
				'id' => $id 
		) );
		if ($result === true) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see 企业信息管理-人员信息管理列表-编辑
	 */
	function actionPersonnelEdit() {
		if (isset ( $_GET ['id'] ) && is_numeric ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$fld = array (
					'id',
					'enterprise_id', // 所在公司ID
					'name', // 人员姓名
					'birthday', // 出生日期
					'card_number', // 身份证号
					'level', // 职称等级
					'profession', // 专业
					'educational_background', // 学历
					'sex', // 性别
					'job_title', // 职称登记
					'job_title_number', // 职称批准文号
					'remark',
					'(select firm_name from company where id = enterprise_id limit 1) as company_name' 
			);
			fb ( $model );
			$service = new PersonnelEntity ();
			$model = $service->get ( $id, $fld );
			if (false === $model || is_array ( $model ) === false || count ( $model ) == 0) {
				// 参数错误
				exit ( "参数错误" );
			} else {
				$this->render ( 'personnelEdit', array (
						'model' => $model 
				) );
			}
		} else {
			exit ( "参数错误" );
		}
	}
	/**
	 *
	 * @see 企业信息管理-人员信息管理列表-详情
	 */
	public function actionPersonneldetail() {
		if (isset ( $_GET ['id'] ) && is_numeric ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$service = new PersonnelEntity ();
			$fld = array (
					'id',
					'enterprise_id', // 所在公司ID
					'name', // 人员姓名
					'birthday', // 出生日期
					'card_number', // 身份证号
					'level', // 职称等级
					'profession', // 专业
					'educational_background', // 学历
					'sex', // 性别
					'job_title', // 职称登记
					'job_title_number', // 职称批准文号
					'remark',
					'(select firm_name from company where id = enterprise_id limit 1) as company_name' 
			);
			
			$model = $service->get ( $id, $fld );
			if (false === $personnel) {
				// 参数错误
				exit ( "参数错误1" );
			} else {
				$this->renderPartial ( 'personneldetail', array (
						'model' => $model 
				) );
			}
		} else {
			exit ( "参数错误2" );
		}
	}
	
	/**
	 *
	 * @see ajax 删除人员信息
	 */
	function actionAjax_person_delete() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new PersonnelEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
}

	
